package cn.xspace.wnx.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.xspace.android.xafcommon.base.BaseActivity;
import com.xspace.android.xafcommon.been.BaseReturnBean;
import com.xspace.android.xafcommon.util.Constants;
import com.xspace.android.xafcommon.util.EditTextInputFilter;
import com.xspace.android.xafcommon.util.LogUtil;
import com.xspace.android.xafcommon.util.T;

import java.util.HashMap;
import java.util.Map;

import cn.xspace.app.view.TitleBar;
import cn.xspace.library.util.MyInfoUtil;
import cn.xspace.library.util.OkHttpManager;
import cn.xspace.wnx.R;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class AddFriendInfoActivity extends BaseActivity {

    private TitleBar mTitleBar;
    private EditText mEtInput;
    private TextView mTvClearEdit;
    private String friend_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void getIntentData() {
        friend_id = getIntent().getStringExtra("friend_id");
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_add_friend_info);
    }

    @Override
    protected void initFindViewById() {
        mTitleBar = (TitleBar) findViewById(R.id.title_bar);
        mTitleBar.showLeftText();
        mTitleBar.showRightText();
        mTitleBar.setRightLayoutTextView(getResources().getString(R.string.send));

        mEtInput = (EditText) findViewById(R.id.et_input);
        mEtInput.setText(String.format(getResources().getString(R.string.verification_info3), MyInfoUtil.getName(mContext)));
        mTvClearEdit = (TextView) findViewById(R.id.tv_clear_edit);
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void initEvent() {
        mTvClearEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mEtInput.setText("");
            }
        });
        mTitleBar.setLeftLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mTitleBar.setRightLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String newValue = mEtInput.getText().toString();
                int chineseCount = EditTextInputFilter.getChineseCount(newValue);
                float sum = (newValue.length() - chineseCount) / 2.0f + (float) chineseCount;
                LogUtil.d("sum " + sum);
                if (sum > 20.0f) {
                    T.showShort(mContext, "不要超过20个字");
                    return;
                }
                if (TextUtils.isEmpty(newValue)) {
                    T.showShort(mContext, "请输入验证信息");
                    return;
                }
                requestAddNewFriend(newValue);
            }
        });
    }

    /**
     * 添加好友的申请
     *
     * @param newValue
     */
    private void requestAddNewFriend(String newValue) {
        showWaitDialog();
        Map<String, Object> requestMap = new HashMap<>();
        requestMap.put("user_id", MyInfoUtil.getMyUserid(mContext));
        requestMap.put("friend_id", friend_id);
        requestMap.put("req_desc", newValue);
        registerSubscription(OkHttpManager.getInstance().commonRequest(Constants.ADD_FRIEND_REQUEST, requestMap)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<BaseReturnBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        hideWaitDialog();
                        e.printStackTrace();
                        T.showShort(mContext, e.getMessage());
                    }

                    @Override
                    public void onNext(BaseReturnBean baseReturnBean) {
                        hideWaitDialog();
                        T.showShort(mContext, "好友申请发送成功");
                        finish();
                    }
                }));
    }
}
